<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use DB;
class ListController extends Controller
{
	//遍历商品
    public function getSearch(Request $request)
    {
    	// dd($request->all());
        $data['zkprice'] = '';
        $data['scnt'] = '';
        $data['status'] = '';
        $type = '';
        $type['tname'] = '';
        $data['gbrand'] = $request->input('gbrand');
        //查询方式判断
        if($request->input('id')){
            //分类查询
            $id = $request->input('id');
            //获取分类
            $path = DB::table('shop_type')->where('tid',$id)->value('path');
            $type['tid'] = explode(',',$path);
            foreach($type['tid'] as $k=>$v){
                $type['tname'][$k] = DB::table('shop_type')->where('tid',$v)->value('tname');
            }
            // 1.查询所有的所有的子类
            $n=DB::table('shop_type')->where('path','like',"%,{$id}%")->lists('tid');
            //加入当前类
            $n[] = $id;
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->whereBetween('status',[0,1])->wherein('tid',$n)->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->whereBetween('status',[0,1])->wherein('tid',$n)->paginate(12);
            }

            // dd($goods);
            $key = null;
        }else{
            //模糊查询
            $key = $request->input('key');
            $keys = '%'.$key.'%';
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->where('gname','like',$keys)->wherein('status',[1,0])->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->where('gname','like',$keys)->wherein('status',[1,0])->paginate(12);
            }
            // dd($goods);
            $id = null;
        }
        //分页

        
        $request = $request->all();
        //品牌条件为空时
        if(empty($request['gbrand'])){$request['gbrand'] = null;}
        // dd($request);
        //商品总数
        $data['count'] = count($goods);
        //解析模板
    	return view('home.goods.list',['goods'=>$goods,'id'=>$id,'key'=>$key,'request'=>$request,'data'=>$data,'type'=>$type]);
    }

    //折扣后价格排序
    public function getZkprice(Request $request)
    {
        $data['scnt'] = '';
        $data['status'] = '';
        $type = '';
        $type['tname'] = '';
        $data['gbrand'] = $request->input('gbrand');

        //判断为空时  点击为正序  再点为倒序
        $data['zkprice'] = empty($request->input('zkprice'))? 'asc' : $request->input('zkprice') ;
        
        //查询方式判断
        if($request->input('id')){
            //分类查询
            $id = $request->input('id');
            //获取分类
            $path = DB::table('shop_type')->where('tid',$id)->value('path');
            $type['tid'] = explode(',',$path);
            foreach($type['tid'] as $k=>$v){
                $type['tname'][$k] = DB::table('shop_type')->where('tid',$v)->value('tname');
            }
            // 1.查询所有的所有的子类
            $n=DB::table('shop_type')->where('path','like',"%,{$id}%")->lists('tid');
            //加入当前类
            $n[] = $id;
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->whereBetween('status',[0,1])->wherein('tid',$n)->orderBy('zkprice',$data['zkprice'])->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->whereBetween('status',[0,1])->wherein('tid',$n)->orderBy('zkprice',$data['zkprice'])->paginate(12);
            }
            // dd($goods);
            $key = null;
        }else{
            //模糊查询
            $key = $request->input('key');
            $keys = '%'.$key.'%';
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->where('gname','like',$keys)->wherein('status',[1,0])->orderBy('zkprice',$data['zkprice'])->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->where('gname','like',$keys)->wherein('status',[1,0])->orderBy('zkprice',$data['zkprice'])->paginate(12);
            }
            // dd($goods);
            $id = null;
        }

        $request = $request->all();
        //页面显示跳转
        if($data['zkprice']=='asc'){
            $data['zkprice'] = 'desc';
        }else{
            $data['zkprice'] = 'asc';
        }
        $data['count'] = count($goods);
        return view('home.goods.list',['goods'=>$goods,'id'=>$id,'key'=>$key,'request'=>$request,'data'=>$data,'type'=>$type]);
    }
    //销量排序
    public function getScnt(Request $request)
    {
        $data['zkprice'] = '';
        $data['status'] = '';
        $type = '';
        $type['tname'] = '';
        $data['gbrand'] = $request->input('gbrand');

        $data['scnt'] = empty($request->input('scnt'))? '' : $request->input('scnt') ;

        //查询方式判断
        if($request->input('id')){
            //分类查询
            $id = $request->input('id');
            //获取分类
            $path = DB::table('shop_type')->where('tid',$id)->value('path');
            $type['tid'] = explode(',',$path);
            foreach($type['tid'] as $k=>$v){
                $type['tname'][$k] = DB::table('shop_type')->where('tid',$v)->value('tname');
            }
            // 1.查询所有的所有的子类
            $n=DB::table('shop_type')->where('path','like',"%,{$id}%")->lists('tid');
            //加入当前类
            $n[] = $id;
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->whereBetween('status',[0,1])->wherein('tid',$n)->orderBy('scnt',$data['scnt'])->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->whereBetween('status',[0,1])->wherein('tid',$n)->orderBy('scnt',$data['scnt'])->paginate(12);
            }
            // dd($goods);
            $key = null;
        }else{
            //模糊查询
            $key = $request->input('key');
            $keys = '%'.$key.'%';
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->where('gname','like',$keys)->wherein('status',[1,0])->orderBy('scnt',$data['scnt'])->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->where('gname','like',$keys)->wherein('status',[1,0])->orderBy('scnt',$data['scnt'])->paginate(12);
            }
            // dd($goods);
            $id = null;
        }

        $request = $request->all();
        if($data['scnt']=='asc'){
            $data['scnt'] = 'desc';
        }else{
            $data['scnt'] = 'asc';
        }
        $data['count'] = count($goods);
        return view('home.goods.list',['goods'=>$goods,'id'=>$id,'key'=>$key,'request'=>$request,'data'=>$data,'type'=>$type]);
    }
    //最新
    public function getRecent(Request $request)
    {
        $data['zkprice'] = '';
        $data['scnt'] = '';
        $data['status'] = 'asc';
        $type = '';
        $type['tname'] = '';
        $data['gbrand'] = $request->input('gbrand');

        //查询方式判断
        if($request->input('id')){
            //分类查询
            $id = $request->input('id');
            //获取分类
            $path = DB::table('shop_type')->where('tid',$id)->value('path');
            $type['tid'] = explode(',',$path);
            foreach($type['tid'] as $k=>$v){
                $type['tname'][$k] = DB::table('shop_type')->where('tid',$v)->value('tname');
            }
            // 1.查询所有的所有的子类
            $n=DB::table('shop_type')->where('path','like',"%,{$id}%")->lists('tid');
            //加入当前类
            $n[] = $id;
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->whereBetween('status',[0,1])->wherein('tid',$n)->orderBy('status','asc')->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->whereBetween('status',[0,1])->wherein('tid',$n)->orderBy('status','asc')->paginate(12);
            }
            // dd($goods);
            $key = null;
        }else{
            //模糊查询
            $key = $request->input('key');
            $keys = '%'.$key.'%';
            //判断是否有条件
            if($data['gbrand']==null){
                $goods = DB::table('shop_goods')->where('gname','like',$keys)->wherein('status',[1,0])->orderBy('status','asc')->paginate(12);
            }else{
                $goods = DB::table('shop_goods')->where('gbrand',$data['gbrand'])->where('gname','like',$keys)->wherein('status',[1,0])->orderBy('status','asc')->paginate(12);
            }
            // dd($goods);
            $id = null;
        }

        $request = $request->all();
        $data['count'] = count($goods);
        return view('home.goods.list',['goods'=>$goods,'id'=>$id,'key'=>$key,'request'=>$request,'data'=>$data,'type'=>$type]);
    }

}
